package com.jjclub.controller;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jjclub.cache.CheckimgCache;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * 验证码-controller
 * @author libo
 */
@Controller
@RequestMapping("/captcha")
public class CaptchaController {



    /**
     * 随机字符字典
     */
    private static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8',
            '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
            'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm',
            'n', 'p', 'q', 'r', 's', 't', 'u' ,'v', 'w', 'x', 'y', 'z'};

    /**
     * 随机数
     */
    private static Random random = new Random();

    /**
     * 获取4位随机数
     * @return
     */
    private static String getRandomString() {
        StringBuffer buffer = new StringBuffer();
        for(int i = 0; i < 4; i++) {
            buffer.append(CHARS[random.nextInt(CHARS.length)]);
        }
        return buffer.toString();
    }

    /**
     * 获取随机数颜色
     * @return
     */
    private static Color getRandomColor() {
        int r =0;
        int b= 0;
        int g =0;
        while(r*0.299 + g*0.578 + b*0.114 < 192){
            r = random.nextInt(255);
            b = random.nextInt(255);
            g=random.nextInt(255);
        }
        return new Color(r,g,b);
    }

    /**
     * 返回某颜色的反色
     * @param c
     * @return
     */
    private static Color getReverseColor(Color c) {
        return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c.getBlue());
    }

    /**
     * 生成验证码
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value="/getCaptcha", method=RequestMethod.GET)
    public void outputCaptcha(HttpServletRequest request, HttpServletResponse response, String rad)
            throws ServletException, IOException {

        // 设置页面不缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        String randomString = getRandomString(); //生成的验证码

//        Captcha c = new Captcha();
//        c.setCaptchaId(rad);
//        c.setCaptcha(randomString.toUpperCase());
        // Integer id = uService.saveCaptcha(c);//保存验证码到数据库中
//        if(id > 0){    //验证码保存成功
//
//        }else{    //验证码保存失败
//            return;
//        }
        CheckimgCache.addMapValue(rad,randomString.toUpperCase());
        String code = CheckimgCache.getMapValue(rad);
        System.out.println("存code"+code);
        int width = 100;    //验证码图像的宽度
        int height = 34;    //验证码图像的高度

        // 在内存中创建图象
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        for(int i=0; i<randomString.length(); i++){
            Color color = getRandomColor();
            Color reverse = getReverseColor(color);
            g.setColor(color);    //设置字体颜色
            g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25));    //设置字体样式
            g.fillRect(0, 0, width, height);
            g.setColor(reverse);
            g.drawString(randomString, 18, 25);
        }
        //随机生成一些点
        for (int i = 0, n = random.nextInt(100); i < n; i++) {
            g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
        }
        // 随机产生干扰线，使图象中的认证码不易被其它程序探测到
        for (int i = 0; i < 10; i++) {
            g.setColor(getRandomColor());
            final int x = random.nextInt(width-1); // 保证画在边框之内
            final int y = random.nextInt(height-1);
            final int xl = random.nextInt(width);
            final int yl = random.nextInt(height);
            g.drawLine(x, y, x + xl, y + yl);
        }
        g.dispose();    //图像生效
        ImageIO.write(bi, "JPEG", response.getOutputStream());    //输出图片到页面

    }


    @GetMapping("/checkCode")
    @ResponseBody
    public String getYzmCode(HttpServletRequest request){
        String rdm = request.getParameter("rdm");
        String yzmcode = request.getParameter("yzmcode");
        yzmcode = yzmcode.toUpperCase();
        System.out.println("rdm:"+rdm);
        System.out.println("yzmcode:"+yzmcode);
        String code = CheckimgCache.getMapValue(rdm);
        System.out.println("code:"+code);
        String message="success";
        if(!yzmcode.equals(code)){
            message = "验证码无效";
        }
        return message;
    }
}
